package com.fengxu.gmall.service.impl;

import com.fengxu.gmall.bean.UserAddress;
import com.fengxu.gmall.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
// dubbo提供的service注解，用于暴露服务
@com.alibaba.dubbo.config.annotation.Service()
public class UserServiceImpl implements UserService {

    // 模拟从数据库查询用户信息
    @Override
    @HystrixCommand()
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1,"北京市","1","欧鸡鸡","13319448888","Y");
        UserAddress userAddress2 = new UserAddress(2,"北京市","2","罗鸡鸡","18119448888","N");
        if(Math.random() > 0.5){
            throw new RuntimeException("模拟异常");
        }
        return Arrays.asList(userAddress1,userAddress2).stream()
                .filter(userAddress -> userAddress.getUserId().equals(userId))
                .collect(Collectors.toList());
    }
}
